//
//  Qr codeViewController.m
//  weibo
//
//  Created by wudongyang on 16/6/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Qr codeViewController.h"

@interface Qr_codeViewController ()
@property(nonatomic, retain)UIImageView *BJImageView;
@property(nonatomic, retain)UIView *BaiView;
@property(nonatomic, retain)UIImageView *HeadImageView;
@property(nonatomic, retain)UILabel *UserLabel;
@property(nonatomic, retain)UILabel *RegionLabel;// 地区
//@property(nonatomic, retain)UIImageView *GenderImageView;// 性别
@end

@implementation Qr_codeViewController
- (void)dealloc
{
    [super dealloc];
    [_BJImageView release];
    [_BaiView release];
    [_HeadImageView release];
    [_UserLabel release];
    [_RegionLabel release];
//    [_GenderImageView release];
}


#pragma mark -- 使用给定的字符串获得CIImage类型的对象
- (CIImage *)getImageByString:(NSString *)dataString
{
    //首先判断字符串是否合理！   是发生过
    if (!dataString || dataString == nil || [dataString isEqualToString:@""])
    {
        return nil;
    }
    //实例化一个滤镜对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //1、设置filter的默认值，防止之前的设置对本次转化有影响
    [filter setDefaults];
    
    //2、将传入的字符串转换为NSData
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    
    //3、将NSData传递给滤镜（通过KVC的方式，设置inputMessage）
    [filter setValue:data forKey:@"inputMessage"];
    
    //4、由filter对象输出图像
    CIImage *outputImage = [filter outputImage];
    
    //5、返回二维码图像
    return outputImage;
}
#pragma mark -- 对图像进行清晰处理，很关键！
- (UIImage *)excludeFuzzyImageFromCIImage: (CIImage *)image size: (CGFloat)size

{
    
    CGRect extent = CGRectIntegral(image.extent);
    
    //通过比例计算，让最终的图像大小合理（正方形是我们想要的）
    CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
    
    size_t width = CGRectGetWidth(extent) * scale;
    
    size_t height = CGRectGetHeight(extent) * scale;
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    
    CIContext * context = [CIContext contextWithOptions: nil];
    
    CGImageRef bitmapImage = [context createCGImage: image fromRect: extent];
    
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    
    CGContextScaleCTM(bitmapRef, scale, scale);
    
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    //切记ARC模式下是不会对CoreFoundation框架的对象进行自动释放的，所以要我们手动释放
    CGContextRelease(bitmapRef);
    
    CGImageRelease(bitmapImage);
    
    CGColorSpaceRelease(colorSpace);
    
    return [UIImage imageWithCGImage: scaledImage];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的二维码";
    // 背景黑色大图片
    self.BJImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"QrBJ.jpg"]];
    self.BJImageView.frame = self.view.bounds;
    [self.view addSubview:self.BJImageView];
    [_BJImageView release];
    // 二维码上面的白色View
    self.BaiView = [[UIView alloc] init];
    self.BaiView.frame = CGRectMake(30, 70, self.view.frame.size.width - 60, self.view.frame.size.height - 230);
    self.BaiView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.BaiView];
    [_BaiView release];
    // 最下面的一行文字
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor whiteColor];
    label.text = @"扫一扫上面的二维码图案,加我好友";
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(0, self.BaiView.frame.size.height - 50, self.BaiView.frame.size.width, 50);
    label.textColor = [UIColor grayColor];
    label.font =[UIFont systemFontOfSize:12];
    [self.BaiView addSubview:label];
    [label release];
    // 用户头像
    self.HeadImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    self.HeadImageView.frame = CGRectMake( 30, 15, 70, 70);
    self.HeadImageView.backgroundColor = [UIColor grayColor];
    self.HeadImageView.layer.masksToBounds = YES;
    self.HeadImageView.layer.cornerRadius = 10;
    self.HeadImageView.layer.borderWidth = 1;
    [self.BaiView addSubview:self.HeadImageView];
    [_HeadImageView release];
    // 用户名字
    self.UserLabel = [[UILabel alloc] init];
    self.UserLabel.backgroundColor = [UIColor clearColor];
    self.UserLabel.text = @"等待传值";
    self.UserLabel.frame = CGRectMake(105, 15, 100, 35);
    self.UserLabel.textColor = [UIColor blackColor];
    self.UserLabel.font =[UIFont systemFontOfSize:21];
    [self.BaiView addSubview:self.UserLabel];
    [self.UserLabel release];
    // 用户地区
    self.RegionLabel = [[UILabel alloc] init];
    self.RegionLabel.backgroundColor = [UIColor clearColor];
    self.RegionLabel.text = @"等待传值";
    self.RegionLabel.frame = CGRectMake(105, 50, 150, 35);
    self.RegionLabel.textColor = [UIColor grayColor];
    self.RegionLabel.font =[UIFont systemFontOfSize:13];
    [self.BaiView addSubview:self.RegionLabel];
    [self.RegionLabel release];
    // 用户性别图片
//    self.GenderImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
//    self.GenderImageView.frame = CGRectMake( 190, 20, 25, 25);
//    self.GenderImageView.backgroundColor = [UIColor grayColor];
//    [self.BaiView addSubview:self.GenderImageView];
//    [_GenderImageView release];
    
    
    // 二维码
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.BaiView.frame.size.width - 50, self.BaiView.frame.size.width - 50)];
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
    CIImage *ciImage = [self getImageByString:@"http://www.jianshu.com/users/be2c0f5976c1/latest_articles"];
    imageView.image = [self excludeFuzzyImageFromCIImage:ciImage size:200];
    [imageView release];
    // 二维码中间的白色底层小View
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(15, 15, 70, 70);
    view.center = self.view.center;
    view.backgroundColor = [UIColor whiteColor];
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 10;
//    view.center = self.BaiView.center;
    [self.view addSubview:view];
    [view release];
    // 二维码中间的用户头像
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    image.frame = CGRectMake(15, 15, 60, 60);
    image.layer.masksToBounds = YES;
    image.layer.cornerRadius = 10;
    image.layer.borderWidth = 1;
    image.center = self.view.center;
    image.backgroundColor = [UIColor grayColor];
    [self.view addSubview:image];
    [image release];
}




@end
